<template>
  <div :class="className" :id="id" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '490px'
    },
    xaxisdata: {
      type: Array,
      default: []
    },
    seriesdata: {
      type: Array,
      default: []
    },
    chartitle: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      chart: null,
      showSeriesData: [],
      showLegendData: []
    }
  },
  mounted () {
    this.init()
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    init () {
      var allData = this.seriesdata
      for (var i = 0; i < allData.length; i++) {
        this.showSeriesData[i] = {
          name: allData[i].name,
          type: 'line',
          smooth: true,
          symbol: 'circle',
          symbolSize: 5,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 4
            }
          },
          data: allData[i].data
        }
        this.showLegendData[i] = {
          name: allData[i].name,
          icon: 'rect',
          textStyle: { fontWeight: 'bold', color: '#666' }
        }
      }
      if (i >= allData.length) {
        this.initChart()
      }
    },
    initChart () {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        backgroundColor: '#fff',
        title: {
          top: 20,
          text: this.chartitle,
          textStyle: {
            fontWeight: 'normal',
            fontSize: 14,
            color: '#000'
          },
          left: '1%'
        },
        tooltip: { // 提示框
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.5)',
          borderColor: '#c8ac84',
          borderWidth: 1,
          axisPointer: {
            lineStyle: {
              color: '#ccc',
              width: 0
            }
          },
          textStyle: {
            color: '#333'
          }
        },
        legend: { // 图例
          orient: 'horizontal',
          selectedMode: false,
          top: 10,
          x: 'right',
          y: 'top',
          padding: [10, 40, 0, 0],
          itemGap: 20,
          textStyle: {
            fontSize: 12,
            color: '#333'
          },
          data: this.showLegendData
        },
        xAxis: [{
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#c0d0e0'
            }
          },
          axisLabel: {
            textStyle: {
              color: '#333'
            }
          },
          data: this.xaxisdata
        }],
        yAxis: [{
          type: 'value',
          axisLine: {
            show: false
          },
          axisLabel: {
            margin: 20,
            textStyle: {
              fontSize: 14,
              color: '#333'
            }
          },
          splitLine: {
            lineStyle: {
              color: '#eee'
            }
          }
        }],
        series: this.showSeriesData
      })
    }
  },
  watch: {
    xaxisdata (val) {
      this.init()
    }
  }
}
</script>
